using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace WbsEditor
{
    public partial class WbsNodeEditControl : FadingUserControl
    {
        public WbsNodeEditControl()
        {
            InitializeComponent();
        }

        private WbsNode _wbsNode;
        public WbsNode WbsNode { get { return _wbsNode; } set { _wbsNode = value; } }
        public ComboBox WorkItemType { get { return _workItemType; } }
        public TextBox Title { get { return _title; } }
        public ComboBox AssignedTo { get { return _assignedTo; } }
        public TextBox RemainingWork { get { return _remainingWork; } }
        public TextBox CompletedWork { get { return _completedWork; } }
        public TextBox Description { get { return _description; } }
        public ComboBox Iteration { get { return _iterationCombo; } }
        private WbsTfsClient _tfsClient;
        public WbsTfsClient TfsClient { get { return _tfsClient; } set { _tfsClient = value; } }
        public int WorkItemId { get { return _workItemID; } set { SetWorkItemID(value); } }
        public bool HasRefreshedNode { get; set; }
        private bool IsInitializing { get; set; }

        private bool _hasTfsOptions;
        private string _assignedToListFromProject;

        public void DisplayControl()
        {
            InitControls();

            HasRefreshedNode = false;
            //Visible = true;
            FadeIn();

            _title.Focus();
        }

        private void InitControls()
        {
            _title.Text = WbsNode.Title;
            _assignedTo.Text = WbsNode.AssignedTo;
            _workItemType.Text = WbsNode.WorkItemType;
            _description.Text = WbsNode.Description;
            _iterationCombo.Text = WbsNode.Iteration;
            WorkItemId = WbsNode.WorkItemId;

            this._description.Enabled = !WbsNode.IsRoot;
            this._description.Text = WbsNode.IsRoot ? "(can't edit description of root)" : WbsNode.Description;
            
            _remainingWork.Enabled = !WbsNode.IsRoot;
            _completedWork.Enabled = !WbsNode.IsRoot;
            
            _title.Enabled = true;
            _workItemType.Enabled = !WbsNode.IsPublished;

            bool readTfsOptions =
                !_hasTfsOptions &&
                TfsClient.IsConnected &&
                !string.IsNullOrEmpty(TfsClient.TeamProject);
            bool refreshTfsOptions =
                _hasTfsOptions &&
                TfsClient.IsConnected &&
                (_assignedToListFromProject != TfsClient.TeamProject);
            if (readTfsOptions || refreshTfsOptions)
            {
                // Read Assigned To drop down list
                _assignedTo.Items.Clear();
                string[] assignedToNames = TfsClient.GetPossibleAssignedToNames();
                _assignedTo.Items.AddRange(assignedToNames);
                _assignedToListFromProject = TfsClient.TeamProject;
                // Read Work Item Types drop down list
                _workItemType.Items.Clear();
                string[] workItemTypes = TfsClient.GetWorkItemTypes();
                _workItemType.Items.AddRange(workItemTypes);
                // Read iterations
                _iterationCombo.Items.Clear();
                string[] iterationList = TfsClient.GetTeamProjectIterations();
                _iterationCombo.Items.AddRange(iterationList);
                // Remember that we have read from the TFS
                _hasTfsOptions = true;
            }
        }

        private int _workItemID;
        private void SetWorkItemID(int id)
        {
            if (id > 0)
            {
                openWorkItemButton.Text = string.Format("Open #{0}", id);
                openWorkItemButton.Enabled = true;
            }
            else
            {
                openWorkItemButton.Text = "Publish";
                //openWorkItemButton.Enabled = false;
            }
            _workItemID = id;
        }

        public event EventHandler QuitQuickEdit;
        public event EventHandler CancelQuickEdit;
        public event EventHandler<WbsEventArgs> RequestPublishWorkItem;
        public event EventHandler<WbsEventArgs> RequestOpenWorkItem;

        private void Controls_OnKeyDown(object sender, KeyEventArgs e)
        {
            bool quit = false;
            bool cancel = false;
            if (e.KeyCode == Keys.Escape && sender != _description)
            {
                cancel = true;
            }
            else if (e.KeyCode == Keys.Escape && sender == _description)
            {
                DialogResult res = MessageBox.Show(
                    "Do you want to save the changes to this node?", "Confirm",
                    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                if (DialogResult.Yes == res)
                    quit = true;
                else if (DialogResult.No == res)
                    cancel = true;
            }
            else if (e.KeyCode == Keys.Enter && sender != _description)
            {
                quit = true;
                e.Handled = true;
            }

            if (quit)
                QuitQuickEdit(this, new EventArgs());
            else if (cancel)
                CancelQuickEdit(this, new EventArgs());

            if (quit || cancel)
                e.Handled = true;
        }

        private void openWorkItemButton_Click(object sender, EventArgs e)
        {
            QuitQuickEdit(this, new EventArgs());
            WbsEventArgs we = new WbsEventArgs(WbsNode);
            if (!WbsNode.IsPublished)
            {
                RequestPublishWorkItem(this, we);
            }
            else
            {
                RequestOpenWorkItem(this, we);
            }
        }

        private void OnCheckRefreshWbsNode(object sender, EventArgs e)
        {
            if (!WbsNode.IsPublished)
                return;
            if (!TfsClient.IsConnected)
                return;
            if (HasRefreshedNode)
                return;

            if (DialogResult.OK != MessageBox.Show("The node will be refreshed from TFS.", "Information", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
                return;

            TfsClient.RefreshWbsNode(WbsNode.Wbs, WbsNode);
            HasRefreshedNode = true;
            InitControls();
        }
    }
}
